
--[[

--]]
----local logger=logger;
local DispatchService = Class("com.ea.ctrl.DispatchService")
local cjson = require "cjson";
local dispatchserviceimpl = require "com.ea.ctrl.DispatchServiceImpl";
local util_str = require "luastar.util.str"


function DispatchService:init()
end

function DispatchService:doService(request, response)

   local v_dispatchserviceimpl=dispatchserviceimpl:new();
  local resp,err = v_dispatchserviceimpl:doService(request,response,ngx.var.uri);
  if not resp then
    response:responseJson( cjson.encode({state=4020002,msg="系统繁忙，请稍后再试",data=nil}));
    --ngx.log(ngx.ERR,logger.formatMsg(string.format("连接服务器 出错,原因",err)));
    return;
  end;
  if ngx.var.APP_DEBUG_LOG== "true" then ngx.log(ngx.DEBUG,logger.formatMsg(resp.status)); end
  if ngx.var.APP_DEBUG_LOG== "true" then ngx.log(ngx.DEBUG,logger.formatMsg(resp.body)); end
  if resp.status == 200 then    
    local result = resp.body;
    if util_str.startsWith(resp.headers["Content-Type"],"image/jpeg") then
      response:responseJpeg(result);
      return;
    elseif util_str.startsWith(resp.headers["Content-Type"],"image/png")then
      response:responsePNG(result);
      return;
    --[[
                         兼容老系统的问题，只要不是图片的结果，则返回json串
        elseif util_str.startsWith(resp.headers["Content-Type"],"application/json") then
    ]]
    else
      response:responseJson(result);
      return;
    --[[else
      response:write(result);
      response:finish();
      return;]]
    end;
  else
    response:responseJson( cjson.encode({state=4020002,msg="系统繁忙，请稍后再试",data=nil}));
    ngx.log(ngx.INFO,logger.formatMsg(string.format("后台目标主机响应异常。URI %s  status=%s",ngx.var.uri,resp.status)));
    return;
  end;
      
     
    
end

return  DispatchService;